module Videre
  module Daemon
    def set_signal_handlers
      # create a signal handler for normal stops, much nicer than a traceback
      normal_exit=Proc.new {
        if $debug 
          puts "stop requested, shutting down"
        end
        self.log.notice "stop requested, shutting down"
        exit 0
      }
      # couple the proc object to the most common signals
      trap 'SIGINT', normal_exit
      trap 'SIGTERM', normal_exit
    end

    def open_logger
      self.log=Syslog.open(self.progname)
    end

    def become_daemon
      Daemons.daemonize unless $debug
      $0=self.progname
    end
  end
end
